//index.js
import api from "../../utils/apis.js";
import message from "../../utils/message.js";
import util from "../../utils/util.js";

//获取应用实例
const app = getApp();

Page({
  data: {
    walletInfo: {},
    inviteUserId: null,
    isPhoneShow: false,

    phoneValue: null,
    verifyCode: null,
    countNum: 60
  },

  onLoad: function(options) {
    let self = this
    if (
      options != null &&
      options != undefined &&
      options.scene != null &&
      options.scene > 0
    ) {
      app.globalData.inviteUserId = options.scene;
    }

    if (
      options != null &&
      options != undefined &&
      options.inviteUserId != null &&
      options.inviteUserId > 0
    ) {
      app.globalData.inviteUserId = options.inviteUserId;
    }

    if (
      options != null &&
      options != undefined &&
      options.scene != null &&
      options.scene > 0
    ) {
      self.setData({
        inviteUserId: options.scene
      });
    }

    if (
      options != null &&
      options != undefined &&
      options.inviteUserId != null &&
      options.inviteUserId > 0
    ) {
      self.setData({
        inviteUserId: options.inviteUserId
      });
    }

    if (app.globalData.inviteUserId) {
      self.setData({
        inviteUserId: app.globalData.inviteUserId
      });
    }

    if (self.data.inviteUserId) {
      self.bindRelationshipAPi();
    }

    wx.startPullDownRefresh();
  },

  onShow: function(options) {},

  onPullDownRefresh: function() {
    this.loadWalletData();
  },

  loadWalletData() {
    let self = this;

    let token = wx.getStorageSync("authorization");

    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: api.walletInfoApi,
      method: "GET",
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (res.statusCode == "200") {
          let data = res.data;
          self.setData({
            walletInfo: data
          });
        } else {
          util.errHandler(res);
        }
      }
    });
  },

  ////////邀请关系///////
  bindRelationshipAPi() {
    let self = this;

    let token = wx.getStorageSync("authorization");
    let scene = "";

    if (self.data.inviteUserId) {
      scene = self.data.inviteUserId;
    }

    wx.request({
      url: api.relationshipAPi + "?scene=" + scene,
      method: "POST",
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        if (res.statusCode == 200) {
          message.showInfo("邀约成功");
        } else {
          util.errHandler(res);
        }
      }
    });
  },

  //监听手机号码的输入
  bindKeyInput: function(e) {
    this.setData({
      phoneValue: e.detail.value
    });
  },
  //监听验证码的输入
  bindCodeInput: function(e) {
    this.setData({
      verifyCode: e.detail.value
    });
  },
  cancelPhone() {
    this.setData({
      isPhoneShow: false
    });
  },

  //发送验证码
  sendSmsAction: function() {
    let self = this;
    if (!self.data.phoneValue) {
      wx.showToast({
        title: "手机号不能为空",
        icon: "",
        image: "/images/logo/warning.png",
        duration: 2000
      });
      return;
    }

    if (self.data.phoneValue.length != 11) {
      wx.showToast({
        title: "手机号不正确",
        icon: "",
        image: "/images/logo/warning.png",
        duration: 2000
      });
      return;
    }

    let param = {
      mobile: self.data.phoneValue
    };

    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url: api.sendSmsCode,
      method: "POST",
      data: param,
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success(res) {
        wx.hideLoading();
        message.showSuccess("验证码已发送");
        if (res.statusCode == "200") {
          let temp = setInterval(() => {
            if (self.data.countNum == 0) {
              self.setData({
                countNum: 60
              });

              self.setData({
                countText: "发送验证码"
              });

              if (
                self.data.intervalNum != null &&
                self.data.intervalNum != undefined
              ) {
                clearInterval(self.data.intervalNum);
              }
            } else {
              let num2 = self.data.countNum;
              num2 -= 1;
              self.setData({
                countNum: num2
              });
            }
          }, 1000);

          self.setData({
            intervalNum: temp
          });
        } else {
          util.errHandler(res);
        }
      }
    });
  },

  bindPhoneSure() {
    let self = this;

    if (!self.data.phoneValue) {
      wx.showToast({
        title: "手机号不能为空",
        icon: "",
        image: "/images/logo/warning.png",
        duration: 2000
      });
      return;
    }

    if (self.data.phoneValue.length != 11) {
      wx.showToast({
        title: "手机号不正确",
        icon: "",
        image: "/images/logo/warning.png",
        duration: 2000
      });
      return;
    }

    if (!self.data.verifyCode) {
      wx.showToast({
        title: "验证码不能为空",
        icon: "",
        image: "/images/logo/warning.png",
        duration: 2000
      });
      return;
    }

    let token = wx.getStorageSync("authorization");

    wx.showLoading({
      title: "加载中"
    });
    wx.request({
      url:
        api.bindPhoneApi +
        "?mobile=" +
        self.data.phoneValue +
        "&smsCode=" +
        self.data.verifyCode,
      method: "POST",
      header: {
        Authorization: "Bearer " + token
      },
      success(res) {
        wx.hideLoading();
        if (res.statusCode == 200) {
          wx.showToast({
            title: "已绑定手机"
          });

          self.setData({
            phone: self.data.phoneValue,
            isPhoneShow: false
          });

          let token = res.data.id_token;
          try {
            wx.setStorageSync("authorization", token);
          } catch (e) {
            console.log(e);
          }
        } else {
          util.errHandler(res);
        }
      }
    });
  },

  checkInfo() {
    wx.navigateTo({
      url: "/pages/index/indexDetail"
    });
  }
});
